package com.lg.edu.service.impl;

import com.lg.edu.dao.ValidCodeDao;
import com.lg.edu.pojo.LgAuthCode;
import com.lg.edu.service.NotifierService;
import com.lg.edu.service.ValidCodeService;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class ValidCodeServiceImpl implements ValidCodeService {

    @Autowired
    private ValidCodeDao validCodeDao;

    @Reference(timeout = 300000)
    private NotifierService notifierService;

    @Override
    public Boolean create(String emailAddr) {
        int i = (int) ((Math.random() * 9 + 1) * 100000);
        String randomValidCode = String.valueOf(i);

        Date now = new Date();
        Date expireTime = new Date(now.getTime() + 600000);

        // 发送
        Boolean sendResult = notifierService.sendMsg(emailAddr, randomValidCode);
        if (!sendResult) {
            return false;
        }

        LgAuthCode lgAuthCode = new LgAuthCode();
        lgAuthCode.setEmail(emailAddr);
        lgAuthCode.setCode(randomValidCode);
        lgAuthCode.setCreatetime(now);
        lgAuthCode.setExpiretime(expireTime);

        validCodeDao.save(lgAuthCode);
        return true;
    }

    @Override
    public Integer validate(String email, String code) {
        if (!isExist(email, code)) {
            return 1;
        }

        if (isExpire(email, code)) {
            return 2;
        }

        return 0;
    }

    public Boolean isExpire(String email, String code) {
        LgAuthCode lgAuthCode = new LgAuthCode();
        lgAuthCode.setCode(code);
        lgAuthCode.setEmail(email);
        Example<LgAuthCode> example = Example.of(lgAuthCode);
        List<LgAuthCode> codeList = validCodeDao.findAll(example);

        Date expiretime = codeList.get(0).getExpiretime();
        LgAuthCode lastestAuthCode = codeList.get(0);
        for (LgAuthCode authCode : codeList) {
            Date e = authCode.getExpiretime();
            if (e.after(expiretime)) {
                expiretime = e;
                lastestAuthCode = authCode;
            }
        }

        return Objects.nonNull(lastestAuthCode) && lastestAuthCode.getCode().equals(code) && expiretime.before(new Date());
    }

    public Boolean isExist(String email, String code) {
        LgAuthCode lgAuthCode = new LgAuthCode();
        lgAuthCode.setCode(code);
        lgAuthCode.setEmail(email);

        System.out.println(">>>>>>>>>>>>> 接收到的email和code分别是：" + email + "    " + code);

        Example<LgAuthCode> example = Example.of(lgAuthCode);
        List<LgAuthCode> codeList = validCodeDao.findAll(example);

        if (codeList.isEmpty()) {
            return false;
        }

        Date expiretime = codeList.get(0).getExpiretime();
        LgAuthCode lastestAuthCode = codeList.get(0);
        for (LgAuthCode authCode : codeList) {
            Date e = authCode.getExpiretime();
            if (e.after(expiretime)) {
                expiretime = e;
                lastestAuthCode = authCode;
            }
        }

        if (Objects.nonNull(lastestAuthCode) && lastestAuthCode.getCode().equals(code)) {
            return true;
        }

        return false;
    }

}
